<template>
  <div class="cart">
    <div class="cart-content" @click="toggleList">
      <div :class="{
          'logo': true,
          'logo-highlight': totalCount
        }">
        <span class="count" v-if="totalCount > 0">{{totalCount}}<span>
      </div>
      <strong class="price">
        <i>&yen;</i> {{totalPrice}}
      </strong>
      <span class="description">
        delivery: {{deliveryPrice}}
      </span>
    </div>
    <div class="cart-option">
      <div class="pay" v-if="totalPrice === 0">
        &yen;{{minPrice}}元起送
      </div>
      <div class="pay" v-else-if="totalPrice <= minPrice">
        还差&yen;{{ minPrice - totalPrice }}元起送
      </div>
      <div class="pay btn-pay" v-else>去支付</div>
    </div>

    <div class="cart-list" v-if="isShowList">
      <div class="header">
        <h2 class="title">Cart</h2>
        <a class="btn btn-remove-all" href="javascript:;" @click="removeAll">Remove All</a>
      </div>
      <div class="body">
        <ul class="list">
          <li class="item" v-for="item in goods">
            <cart-item :item="item"></cart-item>
          </li>
        </ul>
      </div>
    </div>
    <div class="dimmer" v-if="isShowList" @click="isShowList = false"></div>
  </div>
</template>

<script>
import CartItem from 'components/cart/CartItem'
import Count from 'components/common/Count'

export default {
  name: 'Cart',

  components: {
    CartItem,
    Count
  },

  props: {
    goods: {
      type: Array,
      default() {
        return [{
          price: 10,
          count: 0
        }]
      }
    },

    minPrice: {     // 最小起送价格
      type: [Number, String],
      default: 20
    },

    deliveryPrice: {
      type: [Number, String],
      default: 0
    }
  },

  data() {
    return {
      isShowList: false
    }
  },

  computed: {
    totalPrice() {    // 总价
      let total = 0
      this.goods.forEach((item) => {
        total += item.price * item.count
      })
      return total
    },

    totalCount() {    // 总件数
      let count = 0
      this.goods.forEach((item) => {
        count += item.count
      })
      return count
    }
  },

  watch: {
    totalCount(val) {
      if (val <= 0) {
        this.isShowList = false
      }
    }
  },

  methods: {
    toggleList() {
      if (this.totalCount > 0) {
        this.isShowList = !this.isShowList
      }
    },

    removeAll() {
      this.goods.forEach((item) => {
        item.count = 0
      })
    }
  }
}
</script>

<style lang="less" scoped>
@color: rgba(255, 255, 255, 0.4);

.cart {
  z-index: 1500;
  position: fixed;
  left: 0;
  bottom: 0;
  display: flex;
  width: 100%;
  height: 48px;
  background-color: #141d27;
}

.cart-content {
  flex: 1;
  width: 100%;
  font-size: 0;

  .logo {
    z-index: 100;
    display: inline-block;
    position: relative;
    top: -15px;
    width: 60px;
    height: 60px;
    padding: 5px;
    margin: 0 5px 0 15px;
    border-radius: 50%;
    background-color: #141d27;
    color: #fff;

    &::after {
      content: ' ';
      display: block;
      width: 50px;
      height: 50px;
      border-radius: 50%;
      background-color: rgba(255, 255, 255, 0.1);
    }
  }

  .logo-highlight {
    &::after {
      background-color: #41B883;
    }
  }

  .count {
    position: absolute;
    top: 0;
    right: 0;
    width: 24px;
    height: 16px;
    border-radius: 16px;

    line-height: 16px;
    text-align: center;
    font-size: 9px;
    color: #fff;

    background-color: #ff3366;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.5);
  }

  .price, .description {
    display: inline-block;
    padding: 0 10px;
    margin-top: 15px;

    vertical-align: top;
    line-height: 20px;
    color: @color;
  }

  .price {
    border-right: 1px solid @color;
    font-weight: 700;
    font-size: 16px;

    i {
      font-style: normal;
      font-size: 0.75em;
    }
  }

  .description {
    font-size: 12px;
  }
}

.cart-option {
  flex: 0 0 110px;
  width: 110px;
  background-color: #666;

  .pay {
    text-align: center;
    line-height: 50px;
    font-size: 12px;
    background-color: #2b333b;
    color: @color;
  }

  .btn-pay {
    background-color: #41B883;
    color: #fff;
  }
}

.cart-list {
  position: absolute;
  left: 0;
  bottom: 48px;
  width: 100%;
  background-color: #fff;

  .header {
    height: 40px;
    padding: 10px;
    // border-bottom: 1px solid #eee;
    background-color: #eee;

    .title {
      float: left;
      margin: 0;
      font-weight: normal;
      font-size: 18px;
    }

    .btn {
      float: right;
    }
  }

  .body {
    overflow: auto;
    -moz-overflow-scrolling: touch;
    -webkit-overflow-scrolling: touch;
    overflow-scrolling: touch;

    min-height: 70px;
    max-height: 210px;
    padding: 10px 15px;
  }

  .list {
    list-style: none;
    padding: 0;
    margin: 0;

    li {
      padding: 0;
      margin: 0;
      border-bottom: 1px solid #eee;
    }

    li:last-child {
      border-bottom: none;
    }
  }
}

.dimmer {
  z-index: -1;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
}
</style>
